//
//  TagInfoController.m
//  collection
//
//  Created by 靖 张 on 11/22/11.
//  Copyright (c) 2011 Tsinghua University. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "TagInfoController.h"
#import "CollectionDataManager.h"
#import "TagInfo.h"
#import "UserProfileController.h"
#import "AppDelegate.h"
#import "FeedListViewController.h"


@interface TagInfoController()

@property (nonatomic, strong)   IBOutlet    UIScrollView*   subscribedTagScrollView;
@property (nonatomic, strong)   IBOutlet    UIView*         hotTagView;

- (IBAction)onClickTag:(id)sender;
- (IBAction)onClickSearch:(id)sender;

@end


@implementation TagInfoController

@synthesize homepageRootController = _homepageRootController;
@synthesize subscribedTagScrollView = _subscribedTagScrollView;
@synthesize hotTagView  = _hotTagView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"标签";
    }
    
    return self;
}

- (void)dealloc {
    self.homepageRootController = nil;
    self.subscribedTagScrollView = nil;
    self.hotTagView = nil;
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

#define SUBSCRIBEDTAGGAP    12
#define SUBSCRIBEDTAGHEIGHT 49
#define SUBSCRIBEDTAGWIDTH  49

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    // Load subscribed tags and hot tags
    CollectionDataManager* sharedDataManager = [CollectionDataManager sharedInstance];
    NSArray* subscribedTagList = sharedDataManager.hotTagList;
    
    CGRect scrollViewFrame = self.subscribedTagScrollView.frame;
    self.subscribedTagScrollView.contentSize = CGSizeMake([subscribedTagList count] * (SUBSCRIBEDTAGWIDTH + SUBSCRIBEDTAGGAP), scrollViewFrame.size.height);

    self.subscribedTagScrollView.frame = scrollViewFrame;
    
    
    for (int i = 0; i < [subscribedTagList count]; i++) {
        TagInfo* tag = [subscribedTagList objectAtIndex:i];
        
        CGRect frame;
        frame.origin.x      = (SUBSCRIBEDTAGWIDTH + SUBSCRIBEDTAGGAP) * i;
        frame.origin.y      = (scrollViewFrame.size.height - SUBSCRIBEDTAGHEIGHT) / 2;
        frame.size.width    = SUBSCRIBEDTAGWIDTH;
        frame.size.height   = SUBSCRIBEDTAGHEIGHT;
        
        UIButton* tagButton = [[UIButton alloc] initWithFrame:frame];
        //    tagButton.titleLabel.textColor          = [UIColor blackColor];
        tagButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        tagButton.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
        tagButton.contentEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 5);
        [tagButton setBackgroundImage:[UIImage imageNamed:@"tag_49"] forState:UIControlStateNormal];
        [tagButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [tagButton setTitle:tag.name forState:UIControlStateNormal];
        [tagButton addTarget:self action:@selector(onClickTag:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.subscribedTagScrollView addSubview:tagButton];
        [tagButton release];
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.subscribedTagScrollView = nil;
    self.hotTagView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Implementation of UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
}

#pragma mark - Private methods

- (IBAction)onClickTag:(id)sender {
    
    UIButton* button = (UIButton*)sender;
    NSString* tagName = button.titleLabel.text;
    
    NSLog(@"%@", tagName);
    
    FeedListViewController* feedListController = [[FeedListViewController alloc] initWithNibName:@"FeedListViewController" bundle:nil];
    feedListController.tag = tagName;
    AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    [appDelegate.rootNavigationController pushViewController:feedListController animated:YES];
    [feedListController release];
}

- (IBAction)onClickSearch:(id)sender {
    
}

@end
